//
//  PreviewDownFileViewController.m
//  MIS Cloud
//
//  Created by goldeney on 13-12-6.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "PreviewDownFileViewController.h"
#import "PreviewDownFileViewCell.h" 
#import "FileTypeIcon.h"
#import "FilePathHelper.h"
#import "DownLoadHelper.h"
#import "UITableView+Custom.h"

@interface PreviewDownFileViewController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation PreviewDownFileViewController
@synthesize dataArray = _dataArray;
@synthesize pullTableView = _pullTableView;
@synthesize delagate = _delagate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"已下载文件";
        
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成"
                                                              style:UIBarButtonItemStyleDone target:self
                                                              action:@selector(finishCheckButtonEvent:)];
        self.navigationItem.rightBarButtonItem = rightItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pullTableView.tableFooterView = [[UIView alloc] init];
    
    _dataArray = [[NSMutableArray alloc] init];
    [self.pullTableView registerClass:[PreviewDownFileViewCell class] forCellReuseIdentifier:@"PreviewDownFileViewCell"];
    
    [_dataArray removeAllObjects];
    DownLoadHelper *downLoadHelper = [DownLoadHelper sharedInstance];
    [_dataArray addObjectsFromArray:downLoadHelper.finishedlist];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (_dataArray.count <= 0) {
        [self.pullTableView setTableViewNotDataDrawText:@"暂无已下载文件" isShow:YES];
    } else {
        [self.pullTableView setTableViewNotDataDrawText:@"" isShow:NO];
    }
    [self.pullTableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView cellHeightAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0;
}

#pragma mark -
#pragma mark - Draw UITableViewCell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *PreviewDownFileViewCellIdentifier=@"PreviewDownFileViewCell";
    PreviewDownFileViewCell *cell=(PreviewDownFileViewCell *)[tableView dequeueReusableCellWithIdentifier:PreviewDownFileViewCellIdentifier];
    
    FileAttributeModel *fileInfo=[_dataArray objectAtIndex:indexPath.row];
    cell.fileNameLabel.text=fileInfo.fileName;
    cell.fileSizeLabel.text=fileInfo.fileSize;
    cell.fileInfo=fileInfo;
    cell.iconImage.image = [FileTypeIcon setFileTypeIcon:fileInfo.fileName];
    cell.checkButton.checked = fileInfo.isCheck;
    
    return cell;
}


#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //当选择类型为只能选择一个文件的时候
    if (_selectType == PreviewDownFileSelectTypeOne) {
        [self clearAllSelectData:tableView];
    }
    
    //设置当前选择行进行标记
    PreviewDownFileViewCell *cell = (PreviewDownFileViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    FileAttributeModel *fileInfo=[_dataArray objectAtIndex:indexPath.row];
    fileInfo.isCheck = !fileInfo.isCheck;
    cell.checkButton.checked = fileInfo.isCheck;
}

- (void) clearAllSelectData:(UITableView *)tableView
{
    //清理所有选择数据
    for (int i=0;i<_dataArray.count;i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        PreviewDownFileViewCell *cell = (PreviewDownFileViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        
        FileAttributeModel *model = [_dataArray objectAtIndex:i];
        model.isCheck = NO;
        cell.checkButton.checked  = NO;
    }
}

#pragma mark -
#pragma mark IBAction Event

- (IBAction)backButtonEvent:(id)sender
{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)finishCheckButtonEvent:(id)sender
{
    NSMutableArray *dataInfo = [[NSMutableArray alloc] init];
    for (FileAttributeModel *fileInfp in _dataArray) {
        if (fileInfp.isCheck) {
            fileInfp.absolutePath = [[FilePathHelper getTargetFloderPath] stringByAppendingPathComponent:fileInfp.fileName];
            [dataInfo addObject:fileInfp];
        }
    }
    
    if (dataInfo.count<=0) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您没有选择任何文件，请至少选择一个文件" delegate:nil
                                            cancelButtonTitle:@"确定" otherButtonTitles:nil];
        alert.alertViewStyle=UIAlertViewStyleDefault;
        [alert show];
        return;
        
    } else if (self.delagate) {
        if ([self.delagate respondsToSelector:@selector(didPreviewDownFile:data:)])
            [_delagate didPreviewDownFile:self data:dataInfo];
        
        //[self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
